home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / Python 1.1 / Python / modsupport.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-29  |  6.1 KB  |  270 lines  |  [TEXT/KAHL]

  1. /***********************************************************
  2. Copyright 1991, 1992, 1993, 1994 by Stichting Mathematisch Centrum,
  3. Amsterdam, The Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its 
  8. documentation for any purpose and without fee is hereby granted, 
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in 
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI not be used in advertising or publicity pertaining to
  13. distribution of the software without specific, written prior permission.
  14.  
  15. STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
  16. THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17. FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
  18. FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  20. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  21. OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  
  23. ******************************************************************/
  24.  
  25. /* Module support implementation */
  26.  
  27. #include "allobjects.h"
  28. #include "import.h"
  29.  
  30. #ifdef MPW /* MPW pushes 'extended' for float and double types with varargs */
  31. typedef extended va_double;
  32. #else 
  33. typedef double va_double;
  34. #endif
  35.  
  36.  
  37. /* initmodule2() has an additional parameter, 'passthrough', which is
  38.    passed as 'self' to functions defined in the module.  This is used
  39.    e.g. by dynamically loaded modules on the Mac. */
  40.  
  41. object *
  42. initmodule2(name, methods, passthrough)
  43.     char *name;
  44.     struct methodlist *methods;
  45.     object *passthrough; 
  46. {
  47.     object *m, *d, *v;
  48.     struct methodlist *ml;
  49.     char *namebuf;
  50.     if ((m = add_module(name)) == NULL) {
  51.         fprintf(stderr, "initializing module: %s\n", name);
  52.         fatal("can't create a module");
  53.     }
  54.     d = getmoduledict(m);
  55.     for (ml = methods; ml->ml_name != NULL; ml++) {
  56.         namebuf = NEW(char, strlen(name) + strlen(ml->ml_name) + 2);
  57.         if (namebuf == NULL)
  58.             fatal("out of mem for method name");
  59.         sprintf(namebuf, "%s.%s", name, ml->ml_name);
  60.         v = newmethodobject(namebuf, ml->ml_meth,
  61.                     (object *)passthrough, ml->ml_varargs);
  62.         /* XXX The malloc'ed memory in namebuf is never freed */
  63.         if (v == NULL || dictinsert(d, ml->ml_name, v) != 0) {
  64.             fprintf(stderr, "initializing module: %s\n", name);
  65.             fatal("can't initialize module");
  66.         }
  67.         DECREF(v);
  68.     }
  69.     return m;
  70. }
  71.  
  72. /* The standard initmodule() passes NULL for 'self' */
  73.  
  74. object *
  75. initmodule(name, methods)
  76.     char *name;
  77.     struct methodlist *methods;
  78. {
  79.     return initmodule2(name, methods, (object *)NULL);
  80. }
  81.  
  82.  
  83. /* Helper for mkvalue() to scan the length of a format */
  84.  
  85. static int countformat PROTO((char *format, int endchar));
  86. static int countformat(format, endchar)
  87.     char *format;
  88.     int endchar;
  89. {
  90.     int count = 0;
  91.     int level = 0;
  92.     while (level > 0 || *format != endchar) {
  93.         if (*format == '\0') {
  94.             /* Premature end */
  95.             err_setstr(SystemError, "unmatched paren in format");
  96.             return -1;
  97.         }
  98.         else if (*format == '(') {
  99.             if (level == 0)
  100.                 count++;
  101.             level++;
  102.         }
  103.         else if (*format == ')')
  104.             level--;
  105.         else if (level == 0 && *format != '#')
  106.             count++;
  107.         format++;
  108.     }
  109.     return count;
  110. }
  111.  
  112.  
  113. /* Generic function to create a value -- the inverse of getargs() */
  114. /* After an original idea and first implementation by Steven Miale */
  115.  
  116. static object *do_mktuple PROTO((char**, va_list *, int, int));
  117. static object *do_mkvalue PROTO((char**, va_list *));
  118.  
  119. static object *
  120. do_mktuple(p_format, p_va, endchar, n)
  121.     char **p_format;
  122.     va_list *p_va;
  123.     int endchar;
  124.     int n;
  125. {
  126.     object *v;
  127.     int i;
  128.     if (n < 0)
  129.         return NULL;
  130.     if ((v = newtupleobject(n)) == NULL)
  131.         return NULL;
  132.     for (i = 0; i < n; i++) {
  133.         object *w = do_mkvalue(p_format, p_va);
  134.         if (w == NULL) {
  135.             DECREF(v);
  136.             return NULL;
  137.         }
  138.         settupleitem(v, i, w);
  139.     }
  140.     if (v != NULL && **p_format != endchar) {
  141.         DECREF(v);
  142.         v = NULL;
  143.         err_setstr(SystemError, "Unmatched paren in format");
  144.     }
  145.     else if (endchar)
  146.         ++*p_format;
  147.     return v;
  148. }
  149.  
  150. static object *
  151. do_mkvalue(p_format, p_va)
  152.     char **p_format;
  153.     va_list *p_va;
  154. {
  155.     
  156.     switch (*(*p_format)++) {
  157.     
  158.     case '(':
  159.         return do_mktuple(p_format, p_va, ')',
  160.                   countformat(*p_format, ')'));
  161.         
  162.     case 'b':
  163.     case 'h':
  164.     case 'i':
  165.         return newintobject((long)va_arg(*p_va, int));
  166.         
  167.     case 'l':
  168.         return newintobject((long)va_arg(*p_va, long));
  169.         
  170.     case 'f':
  171.     case 'd':
  172.         return newfloatobject((double)va_arg(*p_va, va_double));
  173.         
  174.     case 'c':
  175.         {
  176.             char p[1];
  177.             p[0] = va_arg(*p_va, int);
  178.             return newsizedstringobject(p, 1);
  179.         }
  180.     
  181.     case 's':
  182.     case 'z':
  183.         {
  184.             object *v;
  185.             char *str = va_arg(*p_va, char *);
  186.             int n;
  187.             if (**p_format == '#') {
  188.                 ++*p_format;
  189.                 n = va_arg(*p_va, int);
  190.             }
  191.             else
  192.                 n = -1;
  193.             if (str == NULL) {
  194.                 v = None;
  195.                 INCREF(v);
  196.             }
  197.             else {
  198.                 if (n < 0)
  199.                     n = strlen(str);
  200.                 v = newsizedstringobject(str, n);
  201.             }
  202.             return v;
  203.         }
  204.     
  205.     case 'S':
  206.     case 'O':
  207.         {
  208.             object *v;
  209.             v = va_arg(*p_va, object *);
  210.             if (v != NULL)
  211.                 INCREF(v);
  212.             else if (!err_occurred())
  213.                 /* If a NULL was passed because a call
  214.                    that should have constructed a value
  215.                    failed, that's OK, and we pass the error
  216.                    on; but if no error occurred it's not
  217.                    clear that the caller knew what she
  218.                    was doing. */
  219.                 err_setstr(SystemError,
  220.                        "NULL object passed to mkvalue");
  221.             return v;
  222.         }
  223.     
  224.     default:
  225.         err_setstr(SystemError, "bad format char passed to mkvalue");
  226.         return NULL;
  227.     
  228.     }
  229. }
  230.  
  231. #ifdef HAVE_STDARG_PROTOTYPES
  232. /* VARARGS 2 */
  233. object *mkvalue(char *format, ...)
  234. #else
  235. /* VARARGS */
  236. object *mkvalue(va_alist) va_dcl
  237. #endif
  238. {
  239.     va_list va;
  240.     object* retval;
  241. #ifdef HAVE_STDARG_PROTOTYPES
  242.     va_start(va, format);
  243. #else
  244.     char *format;
  245.     va_start(va);
  246.     format = va_arg(va, char *);
  247. #endif
  248.     retval = vmkvalue(format, va);
  249.     va_end(va);
  250.     return retval;
  251. }
  252.  
  253. object *
  254. vmkvalue(format, va)
  255.     char *format;
  256.     va_list va;
  257. {
  258.     char *f = format;
  259.     int n = countformat(f, '\0');
  260.     if (n < 0)
  261.         return NULL;
  262.     if (n == 0) {
  263.         INCREF(None);
  264.         return None;
  265.     }
  266.     if (n == 1)
  267.         return do_mkvalue(&f, &va);
  268.     return do_mktuple(&f, &va, '\0', n);
  269. }
  270.